import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_learn/router/model/home_router_urls.dart';
import 'package:flutter_learn/router/router_utils.dart';
import 'package:flutter_learn/widgets/custom_star_view.dart';
import 'package:provider/provider.dart';

import '../../utils/inherited_util.dart';
import '../../utils/provider_util.dart';

class TabHome extends StatefulWidget {
  const TabHome({super.key});

  @override
  State<TabHome> createState() => _TabHomeState();
}

class _TabHomeState extends State<TabHome> {
  int _currentCountNummber = 0;

  @override
  Widget build(BuildContext context) {
    return MyInhertedWidget(
      count: _currentCountNummber,
      child: Column(
        children: [
          const Text('InheritedWidget的使用介绍示例：'),
          ShowOne(),
          GestureDetector(
            onTap: () {
              setState(() {
                _currentCountNummber++;
              });
            },
            child: Container(
              color: Colors.blue,
              width: 200,
              height: 100,
              child: Text('点击添加'),
            ),
          ),
          const Text('Provider的使用介绍示例：'),
          ProviderDataShow(),
          FloatingActionButton(
            onPressed: (){
            RouterUtils.instance.push(HomeRouterUrls.homeDetail);
          },
          child: const Text('导航跳转'),)
        ],
      ),
    );
  }
}

class ShowOne extends StatefulWidget {
  @override
  State<ShowOne> createState() => _ShowOneState();
}

class _ShowOneState extends State<ShowOne> {
  @override
  Widget build(BuildContext context) {
    int count = MyInhertedWidget.of(context)?.count ?? 0;
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: [
        Text('当前展示$count'),
      ],
    );
  }
}

class ProviderDataShow extends StatefulWidget {
  @override
  State<ProviderDataShow> createState() => _ProviderDataShowState();
}

class _ProviderDataShowState extends State<ProviderDataShow> {

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    int count = Provider.of<MyProviderModel>(context).count;
    // return Text('当前provider的值==$count');
    return Consumer<MyProviderModel>(builder: (contet, provider, child) {
      return Column(
        children: [
          Text('当前provider的值==$count'),
          Row(
            children: [
              ElevatedButton(
                  onPressed: () {
                    provider.increment();
                  },
                  child: const Text("counter++")),
              ElevatedButton(
                  onPressed: () {
                    provider.decrement();
                  },
                  child: const Text("counter--")),
            ],
          ),
          CustomStarView(rating: 7),
          FittedBox(
            child: Container(
              color: Colors.pink,
              width: 200,
              child: Text('这是内容这是内容这是内容这是内容这是内容这是内容这是内容哈哈', maxLines: 1),
            ),
          ),
          GestureDetector(
            onTap: (){
              print('外部点击');
            },
            child: Container(
              color: Colors.red,
              width: 200,
              height: 200,
              alignment: Alignment.center,
              child: GestureDetector(
                onTap: (){
                  print('内部点击');
                },
                child: Container(
                  color: Colors.yellow,
                  width: 100,
                  height: 100,
                ),
              ),
            ),
          )
        ],
      );
    });
  }
}
